/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mdm.ios.application.impl;

import javax.inject.Inject;
import javax.inject.Named;

import com.foreveross.bsl.mdm.ios.MdmTaskFactory;
import com.foreveross.bsl.mdm.ios.application.MdmCmdApplication;
import com.foreveross.bsl.mdm.ios.application.MdmEnrollApplication;
import com.foreveross.bsl.mdm.ios.application.vo.MdmCommandParamVo;
import com.foreveross.bsl.mdm.ios.application.vo.MdmEnrollmentVo;
import com.foreveross.bsl.mdm.model.ios.domain.MdmTask;

/**
 * @description
 *
 * @author heyaowen
 * @version v1.0
 *
 * @2014-7-18
 *
 */
@Named("iosMdmCmdApplication")
public class MdmCmdApplicationImpl implements MdmCmdApplication {

	@Inject
	private ApnsMessageSender apnsService;

	@Inject
	private MdmEnrollApplication enrollService;

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity
	 * (com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void cmdHandler(String cmdCode, String apnsToken, MdmCommandParamVo cmdValue) {
		// 点击一下按钮如果客户端调用没有错误就代表成功
		if(cmdValue == null) {
			cmdValue = new MdmCommandParamVo();
		}
		/**
		 * 1.根据cmd 执行指令；持久化命令
		 * 2.根据设备唯一 apnsToken 获取pushMagic
		 * 3.唤醒设备，让设备去MDM Server交互
		 */
		MdmEnrollmentVo mdmEnroll = this.enrollService.getEntityByApnsToken(apnsToken);
		MdmTask task = null;
		switch (Integer.parseInt(cmdCode)) {
		case 1:
			task = MdmTaskFactory.createDeviceInformationTask(apnsToken);
			break;
		case 2:
			task = MdmTaskFactory.createDeviceLockCommandTask(apnsToken, 
					 cmdValue.getPin(), cmdValue.getMessage(),  cmdValue.getPhoneNumber());
			break;
		case 3:
			task = MdmTaskFactory.createClearPasscodeCommandTask(apnsToken, mdmEnroll.getUnlockToken());
			break;
		case 4:
			task = MdmTaskFactory.createEraseDeviceCommandTask(apnsToken, cmdValue.getPin());
			break;
		case 5:
			task = MdmTaskFactory.createInstallApplicationListCommandTask(apnsToken, 
					 cmdValue.getIdentifies(), cmdValue.isManagedAppsOnly());
			break;
		case 6:
			task = MdmTaskFactory.createInstallApplicationByiTunesStoreIDTask(apnsToken, cmdValue.getiTunesStoreID());
			break;
		}
		task.save();
		this.apnsService.wakeUpDevice(apnsToken, mdmEnroll.getPushMagic());
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity
	 * (java.lang.String)
	 */
	@Override
	public String getCmds() {
		return "{iosCmds:["
				+ "{cmdName: DeviceInformation, cmdCode: 1, desc: 获取设备信息}, "
				+ "{cmdName: DeviceLock, cmdCode: 2, cmdValue:[pin(String), message(String), phoneNumber(String)], desc: 锁定设备}, "
				+ "{cmdName: ClearPasscode, cmdCode: 3, desc: 清除密码}, "
				+ "{cmdName: EraseDevice, cmdCode: 4, cmdValue:[pin(String)], desc: 擦除设备}, "
				+ "{cmdName: InstallApplicationList, cmdCode: 5, cmdValue:[identifies(List<String>), managedAppsOnly(boolean)], desc: 擦除设备}, "
				+ "{cmdName: InstallApplication, cmdCode: 6, cmdValue:[iTunesStoreID(String)], desc: 安装第三方程序}, "
				+ "{cmdName: InstallApplication, cmdCode: 7, cmdValue:[iTunesStoreID(String)], desc: 安装第三方程序}, "
			+ "]}";
	}
	
//	/** test
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		String token = "fcadbb0c385338c26986577895f0b6d754dfa3baea69d78779cc8bc233830970";
//		MdmTask task = MdmTaskFactory.createDeviceInformationTask(token);
//		task.save();
//		ApnsMessageSender apns = new ApnsMessageSender(); // getBean()
//		apns.wakeUpDevice(token, "C03C05D1-95A1-4B52-8B3B-7DDB0CCC1AFC");
//	}
}
